package org.paniit.welcometoportland;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class Categories extends CustomWindow
{
	public static final String EATING_PLACES = "Eating Places";
	public static final String THINGS_TODO = "Things todo upon arrival in USA";
	public static final String EMERGENCY = "Emergency";
	public static final String ENTERTAINMENT = 	"Entertainment"; 
	public static final String ETHNIC_ASSOCIATION = "Ethnic Associations";
	public static final String SHOPPING = "Shopping";
	public static final String HEALTH = "Health";
	public static final String HOUSING = "Housing";
	public static final String MONEY = "Money";
	public static final String CAR = "Car";
	public static final String CONSUMER = "Consumer Information";
	public static final String EDUCATION = "Education";
	public static final String HELP = "Help";
	public static final String JOBS = "Jobs";
	public static final String KIDS = "Kids";
	public static final String LEGAL = "Legal";
	public static final String NEWS = "News";
	public static final String PUBLIC_TRANSPORT = "Public Transport";
	public static final String TRAVEL = "Travel";
	public static final String INTENT_CATEGORY = "category";
	public static final String INTENT_SUBCATEGORY = "subCategory";
	private ListView categoriesList;
	private String category = "";
	private String subCategory;
	private Intent intent;
	
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		intent = getIntent();
        //Inflate view 
        setContentView(R.layout.categories_view);
        
       //Get reference to UI widgets
        categoriesList = (ListView)findViewById(R.id.categories_list);
        Button previousButton  =  (Button) findViewById(R.id.previousbutton);
        Button nextButton  =  (Button) findViewById(R.id.nextbutton);
        
      //Create Array Adapter to bind to the array to the ListView
        final ArrayAdapter<String> categoriesAdapter = 
	        new ArrayAdapter<String>(this, R.layout.list_item, ROOT_CATEGORIES);
        
      //Bind the array adapter to list view
       categoriesList.setAdapter(categoriesAdapter);
        
       
       //Setting listener
       //categoriesList.setOnCreateContextMenuListener(new MyOnCreateContextMenuListener());
       registerForContextMenu(categoriesList);
       
       previousButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v)
			{
				intent.setClass(Categories.this, Ethnicity.class);
				startActivity(intent);
			}});
       
       nextButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v)
			{
				//Toast.makeText(Categories.this, "The selected category is: " +
					      //category + " subCategory: " + subCategory, Toast.LENGTH_SHORT).show(); 
				intent.setClass(Categories.this, Dummy.class);
				intent.putExtra(INTENT_CATEGORY, category);
				intent.putExtra(INTENT_SUBCATEGORY, subCategory);
				startActivity(intent);
			}});
       
      
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
		   ContextMenu.ContextMenuInfo menuInfo) 
	{
			
		AdapterView.AdapterContextMenuInfo info = 
	            (AdapterView.AdapterContextMenuInfo) menuInfo; 
		category = (String) categoriesList.getItemAtPosition(info.position);
		MenuInflater inflater = getMenuInflater();
		menu.setHeaderTitle(category);
		
		//TODO replace this with switch once we have id's for categories 
		if(category.equals(CAR))
	      	inflater.inflate(R.menu.car_menu, menu);
		if(category.equals(CONSUMER))
			inflater.inflate(R.menu.consumer_info_menu, menu);
		if(category.equals(EATING_PLACES))
			inflater.inflate(R.menu.eating_menu, menu);
		if(category.equals(EMERGENCY))
			inflater.inflate(R.menu.emergency_menu, menu);
		if(category.equals(ENTERTAINMENT))
			inflater.inflate(R.menu.entertainment_menu, menu);
		if(category.equals(ETHNIC_ASSOCIATION))
			inflater.inflate(R.menu.ethnic_menu, menu);
		if(category.equals(EDUCATION))
			inflater.inflate(R.menu.education_menu, menu);
		if(category.equals(HEALTH))
			inflater.inflate(R.menu.health_menu, menu);
		if(category.equals(HELP))
			inflater.inflate(R.menu.help_menu, menu);
		if(category.equals(HOUSING))
			inflater.inflate(R.menu.housing_menu, menu);
		if(category.equals(JOBS))
			inflater.inflate(R.menu.jobs_menu, menu);
		if(category.equals(KIDS))
	    	 inflater.inflate(R.menu.kids_menu, menu);
		if(category.equals(LEGAL))
			inflater.inflate(R.menu.legal_menu, menu);
		if(category.equals(MONEY))
			inflater.inflate(R.menu.money_menu, menu);
		if(category.equals(NEWS))
			inflater.inflate(R.menu.news_menu, menu);
		if(category.equals(PUBLIC_TRANSPORT))
			inflater.inflate(R.menu.public_transportation_menu, menu);
		if(category.equals(SHOPPING))
			inflater.inflate(R.menu.shopping_menu, menu);
		if(category.equals(TRAVEL))
			inflater.inflate(R.menu.travel_menu, menu);    
	}

 		
	public boolean onContextItemSelected(MenuItem item) 
	{  
		subCategory = (String) item.getTitle();
	    return true;  
	}
		

		
	
	
	static final String[] ROOT_CATEGORIES = 
		new String[]{ "Things todo upon arrival in USA",
		  "Eating Places", "Emergency", "Entertainment", "Shopping",
		  "Ethnic Associations", "Health", "Housing", "Money", "Car", 
		  "Consumer Information", "Education", "Help", "Jobs", "Kids",
		  "Legal", "News", "Public Transport", "Travel"};

		                                                  

}
